package com.zhousb.deviceManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhousb.deviceManager.exception.CustomerException;
import com.zhousb.deviceManager.mapper.KiFloorMapper;
import com.zhousb.deviceManager.model.entity.KiFloor;
import com.zhousb.deviceManager.service.FloorService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FloorServiceImpl implements FloorService {
    @Resource
    private KiFloorMapper kiFloorMapper;

    @Override
    public List<KiFloor> listFloor(String clientId) {
        LambdaQueryWrapper<KiFloor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KiFloor::getClientId, clientId);
        return kiFloorMapper.selectList(queryWrapper);
    }

    @Override
    public boolean add(String clientId, String floor) {
        LambdaQueryWrapper<KiFloor> floorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        floorLambdaQueryWrapper.eq(KiFloor::getClientId, clientId)
                .eq(KiFloor::getFloor, floor);
        KiFloor kiFloor = kiFloorMapper.selectOne(floorLambdaQueryWrapper);
        if (kiFloor != null) {
            throw new CustomerException("库房名称【" + floor + "】已存在");
        }
        kiFloor = new KiFloor();
        kiFloor.setClientId(Long.valueOf(clientId));
        kiFloor.setFloor(floor);
        kiFloorMapper.insert(kiFloor);
        return true;
    }

    @Override
    public boolean delete(String id) {
        return kiFloorMapper.deleteById(id) > 0;
    }
}
